import request from '@/utils/request'
import { baseUrl, requestPost, requestGet } from '@/utils/request'

export function loginByUsername(username, password, pos) {

  /*
   const data = {
   username,
   password
   }
   */
  /*
   return request({
   // url: '/login/login',
   url: '/api/api/user/validate',
   method: 'post',
   params: data
   })
   */

  return request({
    // url: '/login/login',
    url: baseUrl + 'system/user/key',
    method: 'post',
    //params: data
  }).then(res => {


    var encrypt = new JSEncrypt();
    encrypt.setPublicKey(res.Data[0]);
    var encrypted = encrypt.encrypt(password);

    // var pwdKey = new RSAUtils.getKeyPair(res.Data[0].exponent, "", res.Data[0].modulus);
    // var reversedPwd = password.split("").reverse().join("");
    // var encrypedPwd = RSAUtils.encryptedString(pwdKey, reversedPwd);

    const data = {
      userName: username,
      userPwd: encrypted,
      uposId: pos,
      sysId: 1
    }
    
    return request({
      // url: '/login/login',
      url: baseUrl + 'system/user/login',
      method: 'post',
      //params: data
      data: data
    })
  })

}

export function logout() {
  return request({
    url: '/login/logout',
    // url: '/api/auth/jwt/invalid',
    method: 'post'
  })
}

export function getUserInfo(token) {
  // return request({
  //   // url: '/user/info',
  //   url: '/api/user/front/info',
  //   method: 'get',
  //   params: {token}
  // })
  // return request(baseUrl + 'system/user/getUserInfo/' + token)
  return request(baseUrl + 'system/user/getUserInfo' + '?time=' + (+new Date()))
}

export function getUserMenus(token) {
  // const data = {
  //     token
  // }

  // return requestPost('/menu/get_menus', data)
  // return request(baseUrl + 'system/user/getUserMenu/' + token)
  return request(baseUrl + 'system/user/getUserMenu' + '?time=' + (+new Date()))
}
